﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExcellOnServices.Models;
using System.Web.Security;
using System.Net;
using System.Data.Entity;

namespace ExcellOnServices.Areas.DashBoard.Controllers
{
    public class AccountController : Controller
    {
        private ExcellOnServicesContext db = new ExcellOnServicesContext();
        //
        // GET: /DashBoard/Company/
        public ActionResult Login()
        {
            //if (!Request.IsAuthenticated)
            //{
            return View();
            //}
            //return RedirectToAction("Index", "Home", new { area = "" });
        }

        // POST: /Register/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Login(Account acc)
        {
            if (ModelState.IsValid)
            {
                string encryptedPass = EncryptionHelper.encrypPassMD5(acc.Password.Trim());

                Account result = db.Accounts.Where(c => c.Roles_Id == 2 && c.Email.Equals(acc.Email) && c.Password.Equals(encryptedPass)).FirstOrDefault();

                if (result != null)
                {
                    if (result.Accounts_Company.Status == Accounts_Company.StatusType.Pending)
                    {
                        ModelState.AddModelError("", "Your account is pending. Please wait until we send a mail for activating.");
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(result.Id.ToString(), true);
                        return RedirectToAction("Index", "Products");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The email and password you entered are incorrect. Please try again (make sure your caps lock is off).");
                    return View();
                }
            }
            return View(acc);
        }
        [Authorize]
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            Session.Abandon();
            return RedirectToAction("Login");
        }

        [Authorize]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Account account = db.Accounts.Find(id);
            if (account == null)
            {
                return HttpNotFound();
            }
            Accounts_Company account_company = account.Accounts_Company;

            return View(account_company);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult Edit([Bind(Include = "Id,BrandName,Address,Telephone,Description")] Accounts_Company account_company)
        {
            if (ModelState.IsValid)
            {
                db.Entry(account_company).State = EntityState.Modified;
                db.SaveChanges();
                TempData["alertMessage"] = "Edited successfully!";
                return View();
            }
            return View(account_company);
        }

        [Authorize]
        public ActionResult ChangePassword(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Account account = db.Accounts.Find(id);
            if (account == null)
            {
                return HttpNotFound();
            }
            return View(account);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult ChangePassword([Bind(Include = "Id,Password")] Account acc, String currentPass, String confirmPass)
        {
            if (String.IsNullOrEmpty(currentPass))
            {
                ModelState.AddModelError("", "Current Password is required.");
            }
            if (String.IsNullOrEmpty(confirmPass))
            {
                ModelState.AddModelError("", "Confirm Password is required.");
            }
            else if (!confirmPass.Equals(acc.Password))
            {
                ModelState.AddModelError("", "Confirm Password is incorrect.");
            }
            else
            {
                string pass = EncryptionHelper.encrypPassMD5(currentPass.Trim());
                Account account = db.Accounts.Where(a => a.Id == acc.Id && a.Password.Equals(pass)).FirstOrDefault();
                if (account == null)
                {
                    ModelState.AddModelError("", "Current Password is incorrect.");
                    return View();
                }
                account.Password = EncryptionHelper.encrypPassMD5(acc.Password.Trim());
                db.SaveChanges();
                TempData["alertMessage"] = "Changed password successfully!";
                return View();
            }
            return View(acc);
        }
    }
}